package com.roadjava.rbac.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.roadjava.rbac.bean.entity.LinkRoleAuthorityDO;
import com.roadjava.rbac.bean.entity.LinkUserRoleDO;
import com.roadjava.rbac.service.LinkRoleAuthorityService;
import com.roadjava.rbac.mapper.LinkRoleAuthorityMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 针对表【link_role_authority(角色和权限关联表)】的数据库操作Service实现
 * @author zhaodaowen
 *
 */
@Service
public class LinkRoleAuthorityServiceImpl extends ServiceImpl<LinkRoleAuthorityMapper, LinkRoleAuthorityDO>
        implements LinkRoleAuthorityService{

    @Override
    public Set<Long> listAuthorityIdByRoleIds(List<Long> roleIds) {
        LambdaQueryWrapper<LinkRoleAuthorityDO> lqw = Wrappers.<LinkRoleAuthorityDO>lambdaQuery()
                .in(LinkRoleAuthorityDO::getRoleId, roleIds);
        List<LinkRoleAuthorityDO> list = list(lqw);
        return list.stream().map(LinkRoleAuthorityDO::getAuthorityId).collect(Collectors.toSet());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByAuthorityId(Long authorityId) {
        LambdaQueryWrapper<LinkRoleAuthorityDO> lqw = Wrappers.<LinkRoleAuthorityDO>lambdaQuery()
                .in(LinkRoleAuthorityDO::getAuthorityId, authorityId);
        remove(lqw);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByRoleId(Long roleId) {
        LambdaQueryWrapper<LinkRoleAuthorityDO> lqw = Wrappers.<LinkRoleAuthorityDO>lambdaQuery()
                .in(LinkRoleAuthorityDO::getRoleId, roleId);
        remove(lqw);
    }

    @Override
    public List<Long> listAuthorityIdByRoleId(Long roleId) {
        LambdaQueryWrapper<LinkRoleAuthorityDO> lqw = Wrappers.<LinkRoleAuthorityDO>lambdaQuery()
                .in(LinkRoleAuthorityDO::getRoleId, roleId);
        List<LinkRoleAuthorityDO> list = list(lqw);
        return list.stream().map(LinkRoleAuthorityDO::getAuthorityId).collect(Collectors.toList());
    }
}




